package com.ninong.ker.admin.rest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ninong.ker.admin.domain.User;
import com.ninong.ker.admin.service.UserService;
import com.ninong.ker.admin.service.dto.UserDTO;
import com.ninong.ker.common.annotation.log.Log;
import com.ninong.ker.common.annotation.perm.CmdAdmin;
import com.ninong.ker.common.response.CmdResponse;
import com.ninong.ker.common.tools.enums.BusinessEnum;

/**
 * 系统部门
 * @author cmd
 *
 */
@RestController
@RequestMapping("/cmd/v1/user")
public class UserResouces {
	
	@Autowired
	private UserService userService;

	
	@Log("用户列表")
	@CmdAdmin({"admin","user:list"})
	@PostMapping
	public ResponseEntity<CmdResponse> list(@RequestBody UserDTO entity)  {
		CmdResponse response = new CmdResponse();
		response.setData(userService.listPage(entity));
		return ResponseEntity.ok(response);
	}
	
	@Log("用户删除")
	@CmdAdmin({"admin","user:delete"})
	@DeleteMapping
	public ResponseEntity<CmdResponse> delete(@RequestBody String[] ids)  {
		userService.delete(ids);
		CmdResponse response = new CmdResponse();
		response.setMessage(BusinessEnum.DELETE_SUCCESS.getMsg());
		return ResponseEntity.ok(response);
	}
	
	@Log("用户新增或编辑")
	@CmdAdmin({"admin","user:add","user:update"})
	@PutMapping
	public ResponseEntity<CmdResponse> create(@RequestBody User entity) throws Exception  {
		userService.create(entity);
		return ResponseEntity.ok(new CmdResponse());
	}
	
	@Log("用户详情")
	@GetMapping(value = "/{id}")
	public ResponseEntity<CmdResponse> get(@PathVariable String id)  {
		CmdResponse response = new CmdResponse();
		response.setData(userService.get(id));
		return ResponseEntity.ok(response);
	}
	
	@Log("激活禁用用户")
	@PostMapping(value = "/enableUser/{id}/{enable}")
	public ResponseEntity<CmdResponse> enableUser(@PathVariable String id,@PathVariable Boolean enable)  {
		CmdResponse response = new CmdResponse();
		userService.enableUser(id,enable);
		return ResponseEntity.ok(response);
	}

	/**
	 * 用户树形集合
	 * @return
	 */
	@GetMapping(value = "/userTree")
	public ResponseEntity<CmdResponse> listUserTree()  {
		CmdResponse response = new CmdResponse();
		response.setData(userService.listUserTree());
		return ResponseEntity.ok(response);
	}

}
